<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>cocoOS: docSemaphores.h File Reference</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">cocoOS
   &#160;<span id="projectnumber">3.0.0</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_semaphores_8h.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">docSemaphores.h File Reference</div>  </div>
</div>
<div class="contents">
<hr/><a name="details" id="details"></a><h2>Detailed Description</h2>
<div class="textblock"><p>cocoOS supports binary and counting semaphores. A semaphore is created in the main function using one of the create functions: <a class="el" href="os__appl_a_p_i_8h.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create()</a> or <a class="el" href="os__appl_a_p_i_8h.html#a9cfb5b3cc72fdaed17009a0cae7fd725">sem_counting_create()</a>. </p>
<div class="fragment"><pre class="fragment">Sem_t bin_sem;
Sem_t count_sem;

<span class="keywordtype">int</span> main( <span class="keywordtype">void</span> ) {
    bin_sem = <a class="code" href="os__appl_a_p_i_8h.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create</a>( initValue );
    count_sem = <a class="code" href="os__appl_a_p_i_8h.html#a9cfb5b3cc72fdaed17009a0cae7fd725">sem_counting_create</a>( maxCount, initValue );
    ...
}
</pre></div><p> <em>initValue</em> is the initial value for the semaphore counter. For a binary semaphore this can be 0 or 1, and for the counting semaphore 0-255. <br/>
</p>
<p><em>maxCount</em> is the maximum value the counting semaphore can reach. <br/>
</p>
<p>A call to <a class="el" href="os__appl_a_p_i_8h.html#ab14d890b777d3b2a9e4c83a3ec15dd31">sem_wait()</a> decrements the semaphore value by one if it is not 0. If it is 0, the value is not decremented, but the task is put into the waiting state. <br/>
</p>
<p>A call to <a class="el" href="os__appl_a_p_i_8h.html#aa5011c236fb03784283e6476e9ae5adb">sem_signal()</a> increments the semaphore value by one if it is not 1 in case of a binary semaphore, or maxCount in case of a counting semaphore. The semaphore is only incremented if no tasks are waiting for the semaphore. If there are a waiting task, the semaphore value is left unchanged and the highest priority waiting task gets the semaphore and is put back to ready state again. <br/>
 <br/>
 <b> Task Synchronization Example </b> <br/>
 <br/>
 In the example below, sem is a semaphore initialized with value 0. Task1 has higher priority than Task2. Task 1 will execute down to the <a class="el" href="os__appl_a_p_i_8h.html#ab14d890b777d3b2a9e4c83a3ec15dd31">sem_wait( sem )</a> call, where it will block because the semaphore value is 0. Then Task2 is scheduled to execute, and will run down to the line where the semaphore is signaled. This will cause the Task2 to block, and Task1 is scheduled to execute due to its higher priority. </p>
<div class="fragment"><pre class="fragment"><span class="keyword">static</span> <span class="keywordtype">void</span> task1( <span class="keywordtype">void</span> ) {
  <a class="code" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open</a>();  
  <span class="keywordflow">for</span> (;;) {
    ...
    <a class="code" href="os__appl_a_p_i_8h.html#ab14d890b777d3b2a9e4c83a3ec15dd31">sem_wait</a>( sem );
    ...
  }
  <a class="code" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close</a>();
} 

<span class="keyword">static</span> <span class="keywordtype">void</span> task2( <span class="keywordtype">void</span> ) {
  <a class="code" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open</a>();  
  <span class="keywordflow">for</span> (;;) {
    ...
    <a class="code" href="os__appl_a_p_i_8h.html#aa5011c236fb03784283e6476e9ae5adb">sem_signal</a>( sem );
    ...
  }
  <a class="code" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close</a>();
}
</pre></div><p><b>Semaphore API</b> The Semaphore API consists of the following functions and macros:</p>
<ul>
<li><a class="el" href="os__appl_a_p_i_8h.html#aaecd95706696b8ff18d5ef74d139d750">sem_bin_create()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#a9cfb5b3cc72fdaed17009a0cae7fd725">sem_counting_create()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#ab14d890b777d3b2a9e4c83a3ec15dd31">sem_wait()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#aa5011c236fb03784283e6476e9ae5adb">sem_signal()</a> </li>
</ul>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="doc_semaphores_8h.html">docSemaphores.h</a>      </li>

    <li class="footer">Generated on Thu Dec 22 2011 21:14:42 for cocoOS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.6 </li>
   </ul>
 </div>


</body>
</html>
